﻿/*
 * Created by SharpDevelop.
 * User: rogerio
 * Date: 19/12/2009
 * Time: 11:43
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using csharp.common;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.common
{
	/// <summary>
	/// Description of frmDefaultSearch.
	/// </summary>
	public partial class frmDefaultSearch : KryptonForm
	{
		protected GenerateSql gsql = new GenerateSql();
		protected List<Field> listFields = new List<Field>();
		protected string _table;
		
		public frmDefaultSearch()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
		}
	
		private void init()
		{
			bool fieldOne= true;
		
			gsql.prepare();
			gsql.commandType = GenerateSql.TCommandType.CMD_SELECT;
			gsql.table = _table;
			
			for(int i=0; i < listFields.Count; i++)
			{
				DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
			
				column.Visible     	= listFields[i].visible;
				column.Name    		= listFields[i].name;
				column.HeaderText	= listFields[i].displayValue;
				column.Width		= listFields[i].size;
				
				dgvMain.Columns.Add(column);
				
				gsql.setValue("",listFields[i].name);
				
				if (listFields[i].marked)
				{
					if (fieldOne)
					{
						rbSearch1.Text = listFields[i].displayValue;
						lblFieldName.Text = rbSearch1.Text;
						fieldOne = false;
					}
					else
					{
						rbSearch2.Text = listFields[i].displayValue;
					}
				}
			}
			
			rbSearch1.Checked = true;
			if (!gsql.execute())
			{
				KryptonMessageBox.Show("Erro ao tentar efetuar consulta ao banco de dados!\n"+gsql.getMsgErro());
				return;
			}
			fillDataGridMain();			
		}
		
		public void addField(Field field)
		{
			listFields.Add(field);
		}
		
		public void clearFields()
		{
			listFields.Clear();
		}
	
		public void setTable(string table)
		{
			_table = table;
		}
		
		void FrmDefaultSearchLoad(object sender, EventArgs e)
		{
			init();
		}
		
		void RbSearch1CheckedChanged(object sender, EventArgs e)
		{
			if (rbSearch1.Checked)
				lblFieldName.Text = rbSearch1.Text;
		}
		
		void RbSearch2CheckedChanged(object sender, EventArgs e)
		{
			if (rbSearch2.Checked)
				lblFieldName.Text = rbSearch2.Text;
		}
		
		private void fillDataGridMain()
		{
			int tam;
			
			String []linha = new String[listFields.Count];
			tam = gsql.getRowCount();
			dgvMain.Rows.Clear();
			for (int i=0; i < tam; i++)
			{
				for(int j=0; j < listFields.Count; j++)
					linha[j]= gsql.getRow(i)[listFields[j].name].ToString();
				dgvMain.Rows.Add(linha);
			}			
		}
	}
}
